from django.db import models
from django.conf import settings
from artworks.models import Artwork


class Like(models.Model):
    """点赞模型"""
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        on_delete=models.CASCADE, 
        related_name='likes',
        db_comment='用户ID'
    )
    artwork = models.ForeignKey(
        Artwork, 
        on_delete=models.CASCADE, 
        related_name='likes',
        db_comment='作品ID'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        db_comment='点赞时间'
    )
    
    class Meta:
        db_table = 'creation_likes'
        db_table_comment = '作品点赞表'
        verbose_name = '点赞'
        verbose_name_plural = '点赞管理'
        unique_together = ['user', 'artwork']
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user']),
            models.Index(fields=['artwork']),
        ]
    
    def __str__(self):
        return f"{self.user.username} 点赞 {self.artwork.title}"


class Comment(models.Model):
    """评论模型"""
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        on_delete=models.CASCADE, 
        related_name='comments',
        db_comment='用户ID'
    )
    artwork = models.ForeignKey(
        Artwork, 
        on_delete=models.CASCADE, 
        related_name='comments',
        db_comment='作品ID'
    )
    content = models.TextField(
        db_comment='评论内容'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        db_comment='评论时间'
    )
    
    class Meta:
        db_table = 'creation_comments'
        db_table_comment = '作品评论表'
        verbose_name = '评论'
        verbose_name_plural = '评论管理'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['user']),
            models.Index(fields=['artwork']),
            models.Index(fields=['created_at']),
        ]
    
    def __str__(self):
        return f"{self.user.username} 评论 {self.artwork.title}"
